package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.LabelAdapter;
import com.dlwx.parttimetalent.adapter.LabelIndustry1Adapter;
import com.dlwx.parttimetalent.adapter.LabelItemAdapter;
import com.dlwx.parttimetalent.adapter.LabelRedAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.bean.IntentClassifyBean;
import com.dlwx.parttimetalent.bean.LabelBean;
import com.dlwx.parttimetalent.bean.LabelConfirmBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.DialogInter;
import com.dlwx.parttimetalent.inter.OnRecyclerViewItemClickListener;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.PricingDialog;
import com.github.jdsjlzx.ItemDecoration.GridItemDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetLabel1211Activity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.recyclerView)
    LRecyclerView chooseRecyclerView;
    @BindView(R.id.list_recyclerView)
    LRecyclerView listRecyclerView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_middle)
    LinearLayout llMiddle;
    @BindView(R.id.sl_middle)
    LinearLayout slMiddle;


//    private Label1211Adapter mAdapter;
//    private ArrayList<Label1211Bean> mDatas;

    private LabelRedAdapter chooseAdapter;
    private ArrayList<LabelBean> chooseDatas;
    private LRecyclerViewAdapter chooselRecyclerViewAdapter;
    private EditText et_title, et_content;
    private TextView tv_cus_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_set_label_1211);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.get(Http.GetIntentList, this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        IntentClassifyBean parentBean = DealResult.getInstance().dealDatas(SetLabel1211Activity.this, response, new TypeToken<IntentClassifyBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            setDatas(parentBean.getBody());
                        } else {
                        }
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(SetLabel1211Activity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("请添加你的工作标签");
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        initChoseRecyclerView();
    }

    private void initChoseRecyclerView() {
        GridLayoutManager manager = new GridLayoutManager(this, 5);
        manager.setAutoMeasureEnabled(true);
        chooseRecyclerView.setLayoutManager(manager);
        chooseAdapter = new LabelRedAdapter(this);
        chooseAdapter.setDataList(new ArrayList<LabelConfirmBean>());
        chooselRecyclerViewAdapter = new LRecyclerViewAdapter(chooseAdapter);
        //divider
        GridItemDecoration divider = new GridItemDecoration.Builder(this)
                .setHorizontal(R.dimen.size_4)
//                 .setVertical(R.dimen.size_4)
                .setColorResource(R.color.white)
                .build();
        chooseRecyclerView.setHasFixedSize(true);
        chooseRecyclerView.addItemDecoration(divider);
        chooseRecyclerView.setAdapter(chooselRecyclerViewAdapter);
        chooseRecyclerView.setLoadMoreEnabled(false);
        chooseRecyclerView.setPullRefreshEnabled(false);
    }
    private void setDatas(List<IntentClassifyBean.BodyBean> beans) {
        IntentClassifyBean.BodyBean nullBean = new IntentClassifyBean.BodyBean();
        nullBean.setClassname("");
//        根据获取到的数据再添加空数据，为自定义布局留出空间
        beans.add(nullBean);
        int size = beans.size();
        for (int i = 0; i < size; i++) {
            IntentClassifyBean.BodyBean bodyBean = beans.get(i);
            if (bodyBean.getLabelclassid() == 15) {
//                行业用人布局
                List<IntentClassifyBean.BodyBean.ChildBeanX> childBeanxs = bodyBean.getChild();
                View view = LayoutInflater.from(this).inflate(R.layout.item_industry, null);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                LRecyclerView recyclerView1 = view.findViewById(R.id.recyclerView1);
                TextView tvTitle = view.findViewById(R.id.tv_title);
                tvTitle.setText(bodyBean.getClassname());
                recyclerView1.setLayoutManager(new LinearLayoutManager(SetLabel1211Activity.this));
                final LabelIndustry1Adapter mAdapter1 = new LabelIndustry1Adapter(SetLabel1211Activity.this);
                mAdapter1.setDataList(childBeanxs);
                final LRecyclerViewAdapter lRecyclerViewAdapter1 = new LRecyclerViewAdapter(mAdapter1);
                recyclerView1.setHasFixedSize(true);
                recyclerView1.setAdapter(lRecyclerViewAdapter1);
                recyclerView1.setLoadMoreEnabled(false);
                recyclerView1.setPullRefreshEnabled(false);
//
                List<IntentClassifyBean.BodyBean.ChildBeanX.ChildBean> mdatas2 = new ArrayList<>();
                mdatas2.addAll(childBeanxs.get(0).getChild());
                final LRecyclerView recyclerView2 = view.findViewById(R.id.recyclerView2);
                recyclerView2.setLayoutManager(new LinearLayoutManager(SetLabel1211Activity.this));
                final LabelAdapter mAdapter2 = new LabelAdapter(SetLabel1211Activity.this);
                mAdapter2.setDataList(mdatas2);
                final LRecyclerViewAdapter lRecyclerViewAdapter2 = new LRecyclerViewAdapter(mAdapter2);
                recyclerView2.setHasFixedSize(true);
                recyclerView2.setAdapter(lRecyclerViewAdapter2);
                recyclerView2.setLoadMoreEnabled(false);
                recyclerView2.setPullRefreshEnabled(false);
                lRecyclerViewAdapter1.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        List<IntentClassifyBean.BodyBean.ChildBeanX> prarentBeans = mAdapter1.getDataList();
                        for (IntentClassifyBean.BodyBean.ChildBeanX bean : prarentBeans) {
                            bean.setChecked(false);
                        }
                        prarentBeans.get(position).setChecked(true);
                        lRecyclerViewAdapter1.notifyDataSetChanged();
                        List<IntentClassifyBean.BodyBean.ChildBeanX.ChildBean> childBeans = mAdapter1.getDataList().get(position).getChild();
                        mAdapter2.clear();
                        lRecyclerViewAdapter2.notifyDataSetChanged();
                        mAdapter2.addAll(childBeans);
                        recyclerView2.refreshComplete(childBeans.size());
                    }
                });
                lRecyclerViewAdapter2.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View view, final int position) {
                        PricingDialog dialog = new PricingDialog(SetLabel1211Activity.this, new DialogInter() {
                            @Override
                            public void getInfo(String data) {
                            }

                            @Override
                            public void getWorkInfo(String time, String price, String unit) {
                                IntentClassifyBean.BodyBean.ChildBeanX.ChildBean bean = mAdapter2.getDataList().get(position);
                                LabelConfirmBean confirmBean = new LabelConfirmBean();
                                confirmBean.setLabelId(bean.getLabelid() + "");
                                confirmBean.setLabelName(bean.getChildname());
                                confirmBean.setTime(time);
                                confirmBean.setPrice(price);
                                List<LabelConfirmBean> chooseConfirmBeans = getChooseConfirmBeans(confirmBean);
                                chooseAdapter.clear();
                                chooselRecyclerViewAdapter.notifyDataSetChanged();
                                chooseAdapter.addAll(chooseConfirmBeans);
                                chooseRecyclerView.refreshComplete(chooseAdapter.getItemCount());
                            }
                        });
                        dialog.show();

                    }
                });
                llMiddle.addView(view);
            } else if (i == (size - 1)) {
//                自定义布局
                View view = LayoutInflater.from(this).inflate(R.layout.item_label3, null);
                view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
                et_title = view.findViewById(R.id.et_title);
                et_content = view.findViewById(R.id.et_content);
                tv_cus_confirm = view.findViewById(R.id.tv_cus_confirm);
                llMiddle.addView(view);
            } else {
//                其他布局
                View view = LayoutInflater.from(this).inflate(R.layout.item_label1, null);
                RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
                TextView tvTitle = view.findViewById(R.id.tv_title);
                tvTitle.setText(bodyBean.getClassname());
                final List<IntentClassifyBean.BodyBean.ChildBeanX> childBeanxs = bodyBean.getChild();
                GridLayoutManager manager = new GridLayoutManager(this, 5);
                manager.setAutoMeasureEnabled(true);
                recyclerView.setLayoutManager(manager);
                LabelItemAdapter mAdapter = new LabelItemAdapter(this, childBeanxs);
                mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener<IntentClassifyBean.BodyBean.ChildBeanX>() {
                    @Override
                    public void onItemClick(View view, final IntentClassifyBean.BodyBean.ChildBeanX data) {
                        PricingDialog dialog = new PricingDialog(SetLabel1211Activity.this, new DialogInter() {
                            @Override
                            public void getInfo(String data) {
                            }
                            @Override
                            public void getWorkInfo(String time, String price, String unit) {
                                LabelConfirmBean confirmBean = new LabelConfirmBean();
                                confirmBean.setLabelId(data.getLabelid() + "");
                                confirmBean.setLabelName(data.getChildname());
                                confirmBean.setTime(time);
                                confirmBean.setPrice(price);
                                List<LabelConfirmBean> chooseConfirmBeans = getChooseConfirmBeans(confirmBean);
                                chooseAdapter.clear();
                                chooselRecyclerViewAdapter.notifyDataSetChanged();
                                chooseAdapter.addAll(chooseConfirmBeans);
                                chooseRecyclerView.refreshComplete(chooseAdapter.getItemCount());
                            }
                        });
                        dialog.show();
                    }
                });
                recyclerView.setHasFixedSize(false);
                recyclerView.setAdapter(mAdapter);
                llMiddle.addView(view);
            }
        }
    }
//  获取已选标签后添加
    private List<LabelConfirmBean> getChooseConfirmBeans(LabelConfirmBean confirmBean) {
        List<LabelConfirmBean> chooseConfirmBeans = new ArrayList<LabelConfirmBean>();
        chooseConfirmBeans.addAll(chooseAdapter.getDataList());
        if (!chooseConfirmBeans.isEmpty()) {
            for (int i = 0; i < chooseConfirmBeans.size(); i++) {
                LabelConfirmBean bean = chooseConfirmBeans.get(i);
                if (TextUtils.equals(bean.getLabelId(), confirmBean.getLabelId())) {
                    chooseConfirmBeans.remove(bean);
                }
            }
        }
        chooseConfirmBeans.add(confirmBean);
        return chooseConfirmBeans;
    }


    @OnClick({R.id.title_left_one_btn, R.id.tv_confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.tv_confirm:
//
                confirm();
                break;
        }
    }

    String sysintent = "";

    private void confirm() {
        List<LabelConfirmBean> beans = chooseAdapter.getDataList();
        if (beans.isEmpty()) {
            ToastUtils.toastForShort(this, "请选择至少一个标签");
            return;
        }
//        id-时间类型-单价@
        for (LabelConfirmBean bean : beans) {
            sysintent = sysintent + bean.getLabelId() + "-" + bean.getTime() + "-" + bean.getPrice() + "@";
        }
        sysintent = sysintent.substring(0, sysintent.length() - 1);
        final String etTitle = et_title.getText().toString();
        final String etContent = et_content.getText().toString();
        if (!TextUtils.isEmpty(etTitle)) {
            PricingDialog dialog = new PricingDialog(SetLabel1211Activity.this, new DialogInter() {
                @Override
                public void getInfo(String data) {
                }

                @Override
                public void getWorkInfo(String time, String price, String unit) {
                    confirm(etTitle, etContent, time, price);
                }
            });
            dialog.show();
        } else {
            confirm("", "", "", "");
        }
    }

    private void confirm(String etTitle, String etContent, String time, String price) {
        loadingDialog.show();
        OkgoUtil.post(Http.InitIntent, this)
                .params("sysintent", sysintent)
                .params("customtitle", etTitle)
                .params("customcontent", etContent)
                .params("customtype", time)
                .params("customprice", price)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        BaseBean parentBean = DealResult.getInstance().dealDatas(SetLabel1211Activity.this, response, new TypeToken<BaseBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if (parentBean.getCode() == Constant.SUCESS) {
                            Intent i=new Intent(SetLabel1211Activity.this, MainActivity.class);
                            SPUtils.put(SetLabel1211Activity.this, Constant.isLogin, true);
                            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            startActivity(i);
                        } else {

                        }
                        ToastUtils.toastForShort(SetLabel1211Activity.this, parentBean.getResult());
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        ToastUtils.toastForShort(SetLabel1211Activity.this, getString(R.string.bad_net));
                    }
                });
    }
}
